﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class ProfileAR : System.Web.UI.Page
{

    public Persons mobjUser
    {
        get
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();

            return Session[ViewState["mobjUser"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();

            Session[ViewState["mobjUser"].ToString()] = value;
        }

    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            checkSecurityAndCustomizePage();
            bindAllControls();
        }
    }


    /// <summary>
    /// this function will bind controls with values
    /// </summary>
    private void bindAllControls()
    {
        lblBadgeBody.Text = mobjUser.Badge;
        if (mobjUser.objParent.GenID >= 0)
            lblEmployeeNameBody.Text = mobjUser.objParent.getFullNameAR();
        else
            lblEmployeeNameBody.Text = mobjUser.getFullNameAR();

        if (mobjUser.objParent.GenID >= 0)
            lblNameBody.Text = mobjUser.getFullNameAR();
        else
        {
            lblNameBody.Visible = false;
            lblName.Visible = false;
        }

        if (mobjUser.objParent.GenID >= 0)
            lblRecordNoBody.Text = mobjUser.ID;
        else
        {
            lblRecordNoBody.Visible = false;
            lblRecordNo.Visible = false;
        }

        lblOrganizationBody.Text = getOrganizationBodyAR();


        lblEmailBody.Text = mobjUser.Email;

        lblPhone1Body.Text = mobjUser.Phone1;
        if (!mobjUser.InnerPhone1.Equals(string.Empty))
            lblPhone1Body.Text += " - " + mobjUser.InnerPhone1;

        lblPhone2Body.Text = mobjUser.Phone2;
        if (!mobjUser.InnerPhone2.Equals(string.Empty))
            lblPhone2Body.Text += " - " + mobjUser.InnerPhone2;

        lblMobile1Body.Text = mobjUser.Mobile1;
        lblMobile2Body.Text = mobjUser.Mobile2;

        txtEmail.Text = mobjUser.Email;
        txtInnerPhone1.Text = mobjUser.InnerPhone1;
        txtInnerPhone2.Text = mobjUser.InnerPhone2;
        txtMobile1.Text = mobjUser.Mobile1;

        txtMobile2.Text = mobjUser.Mobile2;
        txtPhone1.Text = mobjUser.Phone1;
        txtPhone2.Text = mobjUser.Phone2;


    }


    /// <summary>
    /// this function will read all controls
    /// </summary>
    public void readAllControls()
    {
        mobjUser.Email = txtEmail.Text;
        mobjUser.InnerPhone1 = txtInnerPhone1.Text;
        mobjUser.InnerPhone2 = txtInnerPhone2.Text;
        mobjUser.Mobile1 = txtMobile1.Text;

        mobjUser.Mobile2 = txtMobile2.Text;
        mobjUser.Phone1 = txtPhone1.Text;
        mobjUser.Phone2 = txtPhone2.Text;

        if (txtPassword.Text.Trim() != "")
        {
            mobjUser.Password = LMSSecurity.HashPassword(txtPassword.Text.Trim());
        }


    }

    /// <summary>
    /// this function will refresh the user with its values
    /// </summary>
    public void refreshUser()
    {
        PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
        List<Persons> lstPersons = objPersonsDB.getPersonsByGenID(mobjUser.GenID);
        if (lstPersons.Count == 0)
            Response.Redirect("LoginEN.aspx");

        mobjUser = lstPersons[0];



    }


    /// <summary>
    /// this function will  get the organization description
    /// </summary>
    /// <returns>a string containing the description</returns>
    private string getOrganizationBody()
    {
        Persons perOrg = mobjUser;
        if (perOrg.objParent.GenID >= 0)
            perOrg = perOrg.objParent;

        string strOrgan = "";

        OrgnizationChart ocCurrent = perOrg.objOrgnizationChart;

        strOrgan = "<tr><td><div class=\"clearing\" style=\"height:10px;\"></div><div class=\"floating direction aligning\" style=\"width:200px\">" + ocCurrent.objChartType.TypeNameEN + "</div></td><td>" + ocCurrent.NameEN + "</td></tr></table>";

        ocCurrent = ocCurrent.objParent;

        while (ocCurrent.ID >= 0)
        {
            strOrgan = "<tr><td><div class=\"clearing\" style=\"height:10px;\"></div><div class=\"floating direction aligning\" style=\"width:200px\">" + ocCurrent.objChartType.TypeNameEN + "</div></td><td>" + ocCurrent.NameEN + "</td></tr>" + strOrgan;
            ocCurrent = ocCurrent.objParent;
        }


        return "<table cellpadding=\"0\" cellspacing=\"0\">" + strOrgan;


    }



    /// <summary>
    /// this function will  get the organization description
    /// </summary>
    /// <returns>a string containing the description</returns>
    private string getOrganizationBodyAR()
    {
        Persons perOrg = mobjUser;
        if (perOrg.objParent.GenID >= 0)
            perOrg = perOrg.objParent;

        string strOrgan = "";

        OrgnizationChart ocCurrent = perOrg.objOrgnizationChart;

        strOrgan = "<tr><td><div class=\"clearing\" style=\"height:10px;\"></div><div class=\"floating direction aligning\" style=\"width:200px\">" + ocCurrent.objChartType.TypeNameAR + "</div></td><td>" + ocCurrent.NameAR + "</td></tr></table>";

        ocCurrent = ocCurrent.objParent;

        while (ocCurrent.ID >= 0)
        {
            strOrgan = "<tr><td><div class=\"clearing\" style=\"height:10px;\"></div><div class=\"floating direction aligning\" style=\"width:200px\">" + ocCurrent.objChartType.TypeNameAR + "</div></td><td>" + ocCurrent.NameAR + "</td></tr>" + strOrgan;
            ocCurrent = ocCurrent.objParent;
        }


        return "<table cellpadding=\"0\" cellspacing=\"0\">" + strOrgan;


    }





    /// <summary>
    /// this function will check the security and customize the page
    /// </summary>
    private void checkSecurityAndCustomizePage()
    {
        mobjUser = GlobalVars.getCurrentPersonAR();

        if (!mobjUser.lstAllRulesIDs.Contains((long)RulesEnum.Profile_page))
        {
            Response.Redirect("LoginAR.aspx");
        }

        

        pnlEditEdits.Visible = false;
        btnSave.Visible = false;
        btnCancel.Visible = false;

        pnlShowEdits.Visible = true;
        if (!mobjUser.lstAllRulesIDs.Contains((long)RulesEnum.Profile_editing))
        {
            btnEdit.Visible = false;

        }
        else
            btnEdit.Visible = true;


    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        btnEdit.Visible = false;
        btnSave.Visible = true;
        btnCancel.Visible = true;
        pnlEditEdits.Visible = true;
        pnlShowEdits.Visible = false;

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            
            if ((txtRepeatPassword.Text.Trim() == "" || txtPassword.Text.Trim() == "") && txtPassword.Text.Trim() != txtRepeatPassword.Text.Trim())
            {
                lblError.Text = "كلمات المرور غير متطابقة";
                return;
            }

            refreshUser();
            readAllControls();

            PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
            objPersonsDB.Update(mobjUser);

            refreshUser();

            bindAllControls();

            if (!mobjUser.lstAllRulesIDs.Contains((long)RulesEnum.Profile_editing))
            {
                btnEdit.Visible = false;

            }
            else
                btnEdit.Visible = true;
            btnSave.Visible = false;
            btnCancel.Visible = false;
            pnlEditEdits.Visible = false;
            pnlShowEdits.Visible = true;

            lblResult.Text = "&nbsp;تم الحفظ بنجاح.";

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        refreshUser();

        bindAllControls();
        
        if (!mobjUser.lstAllRulesIDs.Contains((long)RulesEnum.Profile_editing))
        {
            btnEdit.Visible = false;

        }
        else
            btnEdit.Visible = true;
        btnSave.Visible = false;
        btnCancel.Visible = false;
        pnlEditEdits.Visible = false;
        pnlShowEdits.Visible = true;

    }
}